﻿Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Gadgeteer.Modules.Edu.Ghi


Namespace GadgeteerApp1
    Partial Public Class Program
        Const dot = 300  ' dot
        Const dash = 900  ' dash is three times as long as a dot
        Const rest = 900 ' rest between letters is three times as long as a dot
        Const gap = 300 ' gap between sounds is as long as one dot
        Const frequency = 300 ' chosen frequency

        Dim melody As Melody = New Melody()



        Dim yPosition As Integer = 2
        Dim yIncrement As Integer = 10
        Dim xPosition As Integer = 2
        Dim xIncrement As Integer = 12
        Dim dotWidth As Integer = 2
        Dim dashWidth As Integer = 1

        Dim shift As Integer = -5 'How far down the alphabet do we want to shift ?


        ' This is run when the mainboard is powered up or reset. 
        Public Sub ProgramStarted()
            Dim newWord As String = "HELLO WORLD"
            Debug.Print("Program Started")

            tunes.Play(melody)

            Dim encodedString As String = encodeString(newWord)

            Dim decodedString As String = decodeString(encodedString)

            Dim font As Font = Resources.GetFont(Resources.FontResources.small)
            display.SimpleGraphics.DisplayText(encodedString, font, GT.Color.Magenta, 2, 10)
            display.SimpleGraphics.DisplayText(decodedString, font, GT.Color.Green, 2, 25)


        End Sub

        Private Function encodeString(text As String) As String
            'Every character is really just an integer - check http://en.wikipedia.org/wiki/ASCII
            'We can take each character and increment it (eg add 1) to get the next character in the ASCII table

            Dim encodedString As String = ""
            For count As Integer = 0 To text.Length() - 1

                Debug.Print(text(count)) ' the original letter
                Dim currentCharacterASCII As Integer = Strings.AscW(text(count)) ' Get the character ASCII value (Number)
                Dim encodedCharacter As String = Strings.ChrW(currentCharacterASCII + shift) ' Add something to the value and then conver to a Character
                Debug.Print("Encoded : " + encodedCharacter)
                encodedString = encodedString + encodedCharacter

            Next
            Return encodedString
        End Function




        Private Sub drawDot()
            melody.Add(frequency, dot)
            melody.Add(0, gap)
            'draw it
            display.SimpleGraphics.DisplayEllipse(GT.Color.Red, xPosition, yPosition, dotWidth, dotWidth)
            xPosition = xPosition + xIncrement
        End Sub

        Private Sub drawDash()
            melody.Add(frequency, dash)
            melody.Add(0, gap)

            'draw it

            display.SimpleGraphics.DisplayLine(GT.Color.Red, dashWidth, xPosition, yPosition, xPosition + dashWidth, yPosition)
            xPosition = xPosition + xIncrement
        End Sub

        Private Sub endOfCharacter()
            yPosition = yPosition + yIncrement
            xPosition = 2
            melody.Add(0, rest)

        End Sub

        Private Function decodeString(encodedString As String) As Object

            Dim decodedString As String = ""
            For count As Integer = 0 To encodedString.Length() - 1

                Debug.Print(encodedString(count)) ' the original letter
                Dim currentCharacterASCII As Integer = Strings.AscW(encodedString(count)) ' Get the character ASCII value (Number)
                Dim decodedCharacter As String = Strings.ChrW(currentCharacterASCII - shift) ' Add something to the value and then conver to a Character
                Debug.Print("Decoded : " + decodedCharacter)
                decodedString = decodedString + decodedCharacter

            Next
            Return decodedString
        End Function

    End Class
End Namespace